<?php

function GetInstagramMedia( $GET )
{
// Client ID for Instagram API
$key = $GET['Access_Token'];
$count = $GET['limit'];
if (($GET['Reset']=='true')||(empty($_SESSION['max_id_instagram'])))
{
	$api = 'https://api.instagram.com/v1/users/self/feed?access_token='.$key.'&count='.$count;

    // Make an API request and create the cache file
    // For example, gets the 32 most popular images on Instagram
	$response = get_curl($api); //change request path to pull different photos
	
	if (count(json_decode($response)->data)!=0) {
	
		$_SESSION['max_id_instagram'] = json_decode($response)->data[count(json_decode($response)->data)-1]->id;
		$_SESSION['min_id_instagram'] = json_decode($response)->data[0]->id;
	}
}
else
{
	$api = 'https://api.instagram.com/v1/users/self/feed?access_token='.$key.'&count='.$count.'&min_id='.$_SESSION['min_id_instagram'];
	$response = get_curl($api);
	$count_second = $count - count(json_decode($response)->data);
	
	$api_second = 'https://api.instagram.com/v1/users/self/feed?access_token='.$key.'&count='.$count_second.'&max_id='.$_SESSION['max_id_instagram'];
	$response_second = get_curl($api_second);
	//var_dump(json_decode($response_second)->data);
	$check_second = json_decode($response_second)->data;
	if (!empty(json_decode($response)->data[0]))
		$_SESSION['min_id_instagram'] = json_decode($response)->data[0]->id;
	//var_dump($query);
	
}

$MediaList = array();

if($response){
	// Decode the response and build an array
	foreach(json_decode($response)->data as $object){
		array_push ($MediaList, MakeMediaArray_Instagram($object));
	}
    //file_put_contents($cache,json_encode($MediaList)); //Save as json
}

if(!empty($check_second))
{
	//print_r($check_second);
	$_SESSION['max_id_instagram'] = json_decode($response_second)->data[count(json_decode($response_second)->data)-1]->id;
	foreach (json_decode($response_second)->data as $object)
	{
		array_push ($MediaList, MakeMediaArray_Instagram($object));
	}
}//End of IF	
//Debug out
//echo "<pre>"; 
//print_r($MediaList);

return $MediaList;
}

function MakeMediaArray_Instagram ($object) {
	$Media = array();
	$id = $object->id;
	
	$WebLink = $object->link;
	$full_name = $object->user->full_name;
	$created_time = $object->created_time;
	$date = date("Y-m-d",$created_time);
	$type = $object->type;
	$MediaDetial = array( "Head"=>"Media", "ID"=>$id, "BeMediaID"=>"Instagram_".$id, "MediaType"=>"", "CommentCount"=>"", "WebLink"=>$WebLink, "Source"=>"Instagram", "Author"=>$full_name,"lastUpdate"=>strtotime($date));
	array_push( $Media, $MediaDetial );
	if((string)$type=='image') {
		$image_url = $object->images->standard_resolution->url;
		$Width = $object->images->standard_resolution->width;
		$Height = $object->images->standard_resolution->height;
			
		$MediaDetial = array( "Head"=>"Image_W_H", "ID"=>$id, "Type"=>'URL', "Width"=>$Width, "Height"=>$Height, "FileName"=>$image_url, "Description"=>"" );
		array_push( $Media, $MediaDetial );
	}
	
	if(isset($object->caption->text)) {
		$Title = $object->caption->text;
		$MediaDetial = array( "Head"=>"Text", "ID"=>"", "Title"=>$Title, "Description"=>"");
		array_push( $Media, $MediaDetial );
	}
	
	
	return $Media;
}

//Added curl for faster response
function get_curl($url){
    if(function_exists('curl_init')){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
        $output = curl_exec($ch);
        echo curl_error($ch);
        curl_close($ch);
        return $output;
    }else{
        return file_get_contents($url);
    }

}
?>